package net.wofly.sale.report.web;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.wofly.common.util.DateUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import net.wofly.common.persistence.PageWrapper;
import net.wofly.common.util.ReturnBean;
import net.wofly.common.web.BaseController;
import net.wofly.sale.report.domain.SaleDailyReport;
import net.wofly.sale.report.dto.DailyReportDetailDTO;
import net.wofly.sale.report.dto.StockDTO;
import net.wofly.sale.report.service.ICardExchangeDailyReportService;
import net.wofly.sale.report.service.ISaleDailyReportService;
import net.wofly.sale.report.service.ISaleStockDailyReportService;
import net.wofly.sale.report.validator.ReportValidator;

@RestController
@RequestMapping("/api/report")
@Api(description = "报表接口")
public class ReportController extends BaseController {
    
    private static final String DEFAULT_PAGE_NUM = "1";
    private static final String DEFAULT_PAGE_SIZE = "10";
    
    @Resource
    ISaleDailyReportService iSaleDailyReportService;
    
    @Resource
    ICardExchangeDailyReportService iCardExchangeDailyReportService;
    
    @Resource
    ISaleStockDailyReportService iSaleStockDailyReportService;
    
    /**
     * 根据日期查询日报
     *
     * @param countDate
     */
    @ApiOperation(value = "根据日期查询日报", notes = "根据日期查询日报")
    @ApiImplicitParams({@ApiImplicitParam(name = "countDate", value = "日期(0点的毫秒数)", required = true, dataType =
            "long", paramType = "form"), @ApiImplicitParam(name = "pageNum", value = "页码（默认第一页）", dataType = "int",
            paramType = "form"), @ApiImplicitParam(name = "pageSize", value = "每页显示条数（默认10条）", dataType = "int",
            paramType = "form")})
    @RequestMapping(value = "/findSaleDailyReportByDate", method = {RequestMethod.POST})
    public ReturnBean<PageWrapper<SaleDailyReport>> findSaleDailyReportByDate(
            Long countDate, @RequestParam(defaultValue = DEFAULT_PAGE_NUM) Integer pageNum,
            @RequestParam(defaultValue = DEFAULT_PAGE_SIZE) Integer pageSize) throws InterruptedException {
        ReturnBean<PageWrapper<SaleDailyReport>> returnBean;
        if (countDate == null || countDate < 0) {
            returnBean = new ReturnBean<>(-1, "日期不能为空，或者日期不正确");
        } else {
            returnBean = iSaleDailyReportService.findSaleDailyReportByDate(countDate, pageNum, pageSize);
        }
        return returnBean;
    }
    
    /**
     * 根据日期查询我的日报
     *
     * @param countDate
     */
    @ApiOperation(value = "根据日期查询我的日报", notes = "根据日期查询我的日报")
    @ApiImplicitParams({@ApiImplicitParam(name = "userID", value = "用户ID/组织机构ID", required = true, dataType = "String", paramType = "form"), 
    						@ApiImplicitParam(name = "countDate", value = "日期(0点的毫秒数)", required = true, dataType ="long", paramType = "form")})
    @RequestMapping(value = "/findMyDailyReportByDate", method = {RequestMethod.POST})
    public ReturnBean<SaleDailyReport> findMyDailyReportByDate(String userID ,Long countDate) throws InterruptedException {
    	
        ReturnBean<SaleDailyReport> returnBean;
        
        if (countDate == null || countDate < 0) {
            returnBean = new ReturnBean<>(-1, "日期不能为空，或者日期不正确");
        } else {
            returnBean = iSaleDailyReportService.findMyDailyReportByDate(userID ,countDate);
        }
        
        return returnBean;
    }


    /**
     * 查询日报详情
     *
     * @param userID
     * @param countDate
     * @return
     */
    @ApiOperation(value = "查询日报详情", notes = "查询日报详情")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "userID", value = "用户ID", required = true, dataType = "String", paramType = "form"),
        @ApiImplicitParam(name = "countDate", value = "日期(0点的毫秒数)", required = true, dataType = "long", paramType = "form")
    })
    @GetMapping(value = "/getSaleDailyReportDetail")
    public ReturnBean<DailyReportDetailDTO> getSaleDailyReportDetail(String userID, Long countDate){
        ReturnBean<DailyReportDetailDTO> returnBean;
        String errorMessage = ReportValidator.validateGetSaleDailyReportDetailParams(userID, countDate);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleDailyReportService.getSaleDailyReportDetail(userID, countDate);
        }
        return returnBean;
    }

    @ApiOperation(value = "业务经理出库汇总", notes = "业务经理出库汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countDate", value = "日期(0点的毫秒数)", required = true, dataType = "long", paramType = "query")
    })
    @GetMapping(value = "/managerOutStockSummary")
    public ReturnBean<List<StockDTO>> managerOutStockSummary(Long countDate){
        ReturnBean<List<StockDTO>> returnBean;
        String errorMessage = ReportValidator.validateManagerOutStockSummaryParams(countDate);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleDailyReportService.managerOutStockSummary(countDate);
        }
        return returnBean;
    }

    @ApiOperation(value = "店面出库汇总", notes = "店面出库汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countDate", value = "日期(0点的毫秒数)", required = true, dataType = "long", paramType = "query")
    })
    @GetMapping(value = "/storeOutStockSummary")
    public ReturnBean<List<StockDTO>> storeOutStockSummary(Long countDate){
        ReturnBean<List<StockDTO>> returnBean;
        String errorMessage = ReportValidator.validateStoreOutStockSummaryParams(countDate);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleDailyReportService.storeOutStockSummary(countDate);
        }
        return returnBean;
    }

    @ApiOperation(value = "业务经理入库汇总", notes = "业务经理入库汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countDate", value = "日期(0点的毫秒数)", required = true, dataType = "long", paramType = "query")
    })
    @GetMapping(value = "/managerInStockSummary")
    public ReturnBean<List<StockDTO>> managerInStockSummary(Long countDate){
        ReturnBean<List<StockDTO>> returnBean;
        String errorMessage = ReportValidator.validateManagerInStockSummaryParams(countDate);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleDailyReportService.managerInStockSummary(countDate);
        }
        return returnBean;
    }

    @ApiOperation(value = "店面入库汇总", notes = "店面入库汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countDate", value = "日期(0点的毫秒数)", required = true, dataType = "long", paramType = "query")
    })
    @GetMapping(value = "/storeInStockSummary")
    public ReturnBean<List<StockDTO>> storeInStockSummary(Long countDate){
        ReturnBean<List<StockDTO>> returnBean;
        String errorMessage = ReportValidator.validateStoreInStockSummaryParams(countDate);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleDailyReportService.storeInStockSummary(countDate);
        }
        return returnBean;
    }
    
    @ApiOperation(value = "生成日报", notes = "生成日报")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countDate", value = "日期(0点的毫秒数)", required = true, dataType = "long", paramType = "query")
    })
    @GetMapping(value = "/generateSaleDailyReport")
    public ReturnBean<String> generateSaleDailyReport(Long countDate){
        
        String errorMessage = ReportValidator.validateStoreInStockSummaryParams(countDate);
        
        if (errorMessage.length() > 0) {
            return new ReturnBean<>(-1, errorMessage);
        }

        Long startTime = DateUtil.processStartTime(new Date()).getTime();
        if (countDate.longValue() != startTime.longValue()) {
            return new ReturnBean<>(-1, "只能生成当前日期的日报！");
        }
        
        iSaleDailyReportService.generateSaleDailyReport(countDate);
        
        iCardExchangeDailyReportService.generateCardExchangeDailyReport(countDate);
        
        iSaleStockDailyReportService.generateSaleStockDailyReport(countDate);
        
        return  new ReturnBean<>(ReturnBean.CODE_SUCCESS, "生成日报成功！") ;
    }


    @ApiOperation(value = "每日终端商品出库汇总", notes = "每日终端商品出库汇总")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "countDate", value = "日期(0点的毫秒数)", required = true, dataType = "long", paramType = "query")
    })
    @GetMapping(value = "/commodityTerminalOutStockSummary")
    public ReturnBean<List<StockDTO>> commodityTerminalOutStockSummary(Long countDate){
        ReturnBean<List<StockDTO>> returnBean;
        String errorMessage = ReportValidator.validateCommodityTerminalOutStockSummaryParams(countDate);
        if (errorMessage.length() > 0) {
            returnBean = new ReturnBean<>(-1, errorMessage);
        } else {
            returnBean = iSaleDailyReportService.commodityTerminalOutStockSummary(countDate);
        }
        return returnBean;
    }
    
}
